<?php

use yii\helpers\Url;

$web = Url::base();
$url = \app\librarys\Tools::urlBase();
?>
<script type="application/javascript" src="<?= $web ?>/js/echarts.js"></script>

<script type="application/javascript">
    $(function () {
        // Workaround for bug in mouse item selection
        $.fn.typeahead.Constructor.prototype.blur = function () {
            var that = this;
            setTimeout(function () {
                that.hide()
            }, 250);
        };

        initRecharge('month');
        initSellAmount('month');
    });

    function initRecharge(type) {
        // 基于准备好的dom，初始化echarts实例
        var rechargeChart = echarts.init(document.getElementById('recharge_charts_panel'));
        var dateAxisData = [];
        var rechargesAxisData = [];

        var data = {};
        data.type = type;
        requestJson("get-recharged-amount-handler", data, true, function (response) {
            switch (type) {
                case "month":
                    type = "(月)";
                    break;
                case "quarter":
                    type = "(季度)";
                    break;
                case "year":
                    type = "(年)";
                    break;
            }
            if (response.status == 0) {
                var datasource = response.data;

                $(datasource).each(function () {
                    dateAxisData.push(this.date);
                    rechargesAxisData.push(this.amount);

                    // 指定图表的配置项和数据
                    option = {
                        title: {
                            text: '付款总金额' + type
                        },
                        tooltip: {
                            trigger: 'axis'
                        },
                        legend: {
                            data: ['总金额']
                        },
                        toolbox: {
                            feature: {
                                myTool1: {
                                    show: true,
                                    title: '按月',
                                    icon: 'image://../../images/chart/month.png',
                                    onclick: function () {
                                        initRecharge("month");
                                    }
                                },
                                myTool2: {
                                    show: true,
                                    title: '按季度',
                                    icon: 'image://../../images/chart/quarter.png',
                                    onclick: function () {
                                        initRecharge("quarter");
                                    }
                                },
                                myTool3: {
                                    show: true,
                                    title: '按年',
                                    icon: 'image://../../images/chart/year.png',
                                    onclick: function () {
                                        initRecharge("year");
                                    }
                                },
                                saveAsImage: {}
                            }
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: [
                            {
                                type: 'category',
                                boundaryGap: true,
                                data: dateAxisData
                            }
                        ],
                        yAxis: [
                            {
                                name: '总金额',
                                type: 'value'
                            }
                        ],
                        series: [
                            {
                                name: '总金额',
                                type: 'bar',
                                label: {
                                    normal: {
                                        show: true,
                                        position: 'top'
                                    }
                                },
                                areaStyle: {normal: {}},
                                data: rechargesAxisData
                            }
                        ]
                    };

                    // 使用刚指定的配置项和数据显示图表。
                    rechargeChart.setOption(option);
                });
            }
        });
    }

    function initSellAmount(type) {
        // 基于准备好的dom，初始化echarts实例
        var salesChart = echarts.init(document.getElementById('sales_charts_panel'));

        var dateAxisData = [];
        var quantityAxisData = [];
        var sellingPriceAxisData = [];

        var data = {};
        data.type = type;
        requestJson("get-sell-amount-handler", data, true, function (response) {
            switch (type) {
                case "month":
                    type = "(月)";
                    break;
                case "quarter":
                    type = "(季度)";
                    break;
                case "year":
                    type = "(年)";
                    break;
            }
            if (response.status == 0) {
                var datasource = response.data;
                $(datasource).each(function () {
                    dateAxisData.push(this.date);
                    quantityAxisData.push(this.quantity);
                    sellingPriceAxisData.push(this.selling_price);
                });

                // 指定图表的配置项和数据
                option = {
                    title: {
                        text: '销售情况' + type
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['销售额', '订单量']
                    },
                    toolbox: {
                        feature: {
                            myTool1: {
                                show: true,
                                title: '按月',
                                icon: 'image://../../images/chart/month.png',
                                onclick: function () {
                                    initSellAmount("month");
                                }
                            },
                            myTool2: {
                                show: true,
                                title: '按季度',
                                icon: 'image://../../images/chart/quarter.png',
                                onclick: function () {
                                    initSellAmount("quarter");
                                }
                            },
                            myTool3: {
                                show: true,
                                title: '按年',
                                icon: 'image://../../images/chart/year.png',
                                onclick: function () {
                                    initSellAmount("year");
                                }
                            },
                            saveAsImage: {}
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            boundaryGap: true,
                            data: dateAxisData
                        }
                    ],
                    yAxis: [
                        {
                            name: '销售额',
                            type: 'value'
                        },
                        {
                            name: '订单量',
                            type: 'value'
                        }
                    ],
                    series: [
                        {
                            name: '销售额',
                            type: 'bar',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top'
                                }
                            },
                            areaStyle: {normal: {}},
                            data: sellingPriceAxisData
                        },
                        {
                            name: '订单量',
                            type: 'line',
                            label: {
                                normal: {
                                    show: true,
                                    position: 'top'
                                }
                            },
                            areaStyle: {normal: {}},
                            data: quantityAxisData
                        }
                    ]
                };

                // 使用刚指定的配置项和数据显示图表。
                salesChart.setOption(option);
            }
        });
    }

</script>


<div class="row">
    <div class="col-md-12">
        <!--statistics start-->
        <div class="row state-overview">
            <div class="col-md-3 col-xs-12 col-sm-6">
                <div class="panel blue">
                    <div class="symbol">
                        <i class="fa fa-rmb"></i>
                    </div>
                    <div class="state-value">
                        <?php
                        if ($credit != null) {
                            ?>
                            <div class="title">余额: <i class="fa fa-rmb"></i> <?= $credit['balance'] ?></div>
                            <div class="title">信用额: <i class="fa fa-rmb"></i> <?= $credit['credit_quota'] ?></div>
                            <div class="title">可用额: <i
                                    class="fa fa-rmb"></i> <?= $credit['balance'] + $credit['credit_quota'] ?></div>
                            <?php
                        }
                        ?>
                    </div>
                </div>
            </div>


            <div class="col-md-3 col-xs-12 col-sm-6">
                <div class="panel purple">
                    <div class="symbol">
                        <i class="fa fa-file-text"></i>
                    </div>
                    <div class="state-value">
                        <?php
                        for ($i = 0; $i < count($all_order_list); $i++) {
                            $non_complete_count = $all_order_list[$i]['count'] - $complete_order_list[$i]['count'];
                            $href = $url . '/order/flow-list';
                            if (\app\models\Permission::isSystemAdmin()) {
                                $href = $url . '/order/user-order-list';
                            }
                            switch ($all_order_list[$i]['dateType']) {
                                case 'day':
                                    echo "<div class=\"title\">日订单 <a href='$href'>总数:" . $all_order_list[$i]['count'] . "(未完成:" . $non_complete_count . ")" . "</a></div>";
                                    break;
                                case 'week':
                                    echo "<div class=\"title\">周订单 <a href='$href'>总数:" . $all_order_list[$i]['count'] . "(未完成:" . $non_complete_count . ")" . "</a></div>";
                                    break;
                                case 'month':
                                    echo "<div class=\"title\">月订单 <a href='$href'>总数:" . $all_order_list[$i]['count'] . "(未完成:" . $non_complete_count . ")" . "</a></div>";
                                    break;
                            }
                        }
                        ?>
                    </div>
                </div>
            </div>
            <div class="col-md-3 col-xs-12 col-sm-6">
                <div class="panel red">
                    <div class="symbol">
                        <i class="fa fa-rmb"></i>
                    </div>
                    <div class="state-value">
                        <?php
                        foreach ($order_price_list as $order_price) {
                            switch ($order_price['dateType']) {
                                case 'day':
                                    echo "<div class=\"title\">日销售额: " . $order_price['price'] . "</div>";
                                    break;
                                case 'week':
                                    echo "<div class=\"title\">周销售额: " . $order_price['price'] . "</div>";
                                    break;
                                case 'month':
                                    echo "<div class=\"title\">月销售额: " . $order_price['price'] . "</div>";
                                    break;
                            }
                        }
                        ?>
                    </div>
                </div>
            </div>
            <div class="col-md-3 col-xs-12 col-sm-6">
                <div class="panel blue">
                    <div class="symbol">
                        <i class="fa fa-file-text"></i>
                    </div>
                    <div class="state-value">
                        <div class="title">最近付款日期:
                            <?php
                            echo $last_recharg != null ? date('Y-m-d', strtotime($last_recharg['create_datetime'])) : '';
                            ?>
                        </div>
                        <div class="title">最近付款金额:￥
                            <?php
                            echo $last_recharg != null ? $last_recharg['amount'] : '0.00';
                            ?>
                        </div>
                    </div>
                </div>
            </div>

            <!--            <div class="col-md-3 col-xs-12 col-sm-6">-->
            <!--                <div class="panel green">-->
            <!--                    <div class="symbol">-->
            <!--                        <i class="fa fa-bookmark"></i>-->
            <!--                    </div>-->
            <!--                    <div class="state-value">-->
            <!--                        <div class="title">总订单量: 20</div>-->
            <!--                        <div class="title">&nbsp;</div>-->
            <!--                        <div class="title">总销售额: <i class="fa fa-rmb"></i> 209823</div>-->
            <!--                    </div>-->
            <!--                </div>-->
            <!--            </div>-->
        </div>
        <!--statistics end-->
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        付款总金额
    </div>
    <div class="panel-body">
        <div id="recharge_charts_panel" style="height: 300px;;"></div>
        <script type="text/javascript">


        </script>
    </div>
    <div class="panel-heading">
        销售情况
    </div>
    <div class="panel-body">
        <div id="sales_charts_panel" style="height: 300px;;"></div>
        <script type="text/javascript">

        </script>
    </div>
</div>

<div class="directory-info-row">
    <div class="row">
        <?php
        foreach ($institution_items as $item) {
            ?>
            <div class="col-md-4 col-sm-6">
                <div class="panel">
                    <div class="panel-body">
                        <h4><?= $item['name'] ?> <span class="text-muted small"> - <?= $item['code'] ?> </span></h4>
                        <div class="media">
                            <div class="media-body">
                                <address>
                                    余额: <i class="fa fa-rmb"></i> <?= $item['balance'] ?> <br>
                                    信用额: <i class="fa fa-rmb"></i> <?= $item['credit_quota'] ?> <br>
                                    可用额: <i class="fa fa-rmb"></i> <?= $item['balance'] + $item['credit_quota'] ?><br>
                                    消费额: <i class="fa fa-rmb"></i> <?= $item['total_settlement_price'] ?><br>
                                </address>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php
        }
        ?>
    </div>
</div>